package com.jichangxiu.dynamic.test.config;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jcx
 * @date 2022/11/27 7:42
 */
@Service
public class DataSourceInfoServiceImpl {

    private List<DataSourceInfo> dataSourceInfos = new ArrayList<>();

    {
        dataSourceInfos.add(DataSourceInfo.builder().dbName("t1").url("jdbc:mysql://127.0.0.1:3306/jcx-test-01?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC&allowPublicKeyRetrieval=true").username("root").password("root").build());
        dataSourceInfos.add(DataSourceInfo.builder().dbName("t2").url("jdbc:mysql://127.0.0.1:3306/jcx-test-02?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC&allowPublicKeyRetrieval=true").username("root").password("root").build());
    }


    /**
     * 从数据库中查询配置的数据库链接信息，构建动态的数据源
     */
    public void buildDynamicDataSourceFromDB() throws Exception {
        for (DataSourceInfo info : dataSourceInfos) {
            DynamicDataSourceService.addDataSource(info.getDbName(), info.getUrl(), info.getUsername(), info.getPassword());
        }
    }

}

